package com.vinhvt.effiport.algorithm.genetic;


import com.vinhvt.effiport.algorithm.genetic.bio.Individual;
import com.vinhvt.effiport.algorithm.genetic.bio.WeightGene;
import com.vinhvt.effiport.algorithm.genetic.mapper.GeneMap;
import com.vinhvt.effiport.algorithm.genetic.mapper.MapItem;
import com.vinhvt.effiport.financial.ep.Asset;
import com.vinhvt.effiport.financial.ep.Portfolio;
import com.vinhvt.effiport.financial.ep.Underlying;
import com.vinhvt.effiport.util.Helper;

public class ADNCodex {	
	
	public static Individual cipher(Object obj, GeneMap map) throws ClassNotFoundException {
		Individual individual = new Individual();
		if(obj instanceof Portfolio) {
			Portfolio portfolio = (Portfolio)obj;
			int times = portfolio.getNumberOfAsset();
			for(int i = 0; i < times; i++) {			
				double weight = portfolio.getAssetAt(i).getWeight();
				WeightGene gene = new WeightGene(weight);
				individual.getChrWeight().addGene(gene);		
			}
		}
		return individual;
	}
	
	public static Portfolio decipher(Individual individual, GeneMap map) {
		Portfolio portfolio = new Portfolio();
		int times = individual.getSize();
		for(int i = 0; i < times; i++) {
			MapItem item = map.getMapItem(i);											
			Underlying underlying = new Underlying(item.getName());
			Asset asset = new Asset(underlying);
			
			double w = Helper.round(individual.getChrWeight().getGene(i).getValue().doubleValue());
			portfolio.addAsset(asset, w);
		}
		
		return portfolio;		
	}			
}
